using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
using TuyennvLibrary.SqlClient;
namespace TuyennvLibrary.ServerApi.Models
{
    public class ApplicationDevices
    {
        public int id { get; set; }
        public string appid { get; set; }
        public string deviceid { get; set; }
        public string gcmid { get; set; }
        public DateTime createdate { get; set; }

        public static string COLUMN_ID = "id";
        public static string COLUMN_APPID = "appid";
        public static string COLUMN_DEVICEID = "deviceid";
        public static string COLUMN_GCMID = "gcmid";
        public static string COLUMN_CREATEDATE = "createdate";

        public static string TableName = "ApplicationDevices";

        public ApplicationDevices()
        {
            this.id = 0;
            this.appid = string.Empty;
            this.deviceid = string.Empty;
            this.gcmid = string.Empty;
            this.createdate=DateTime.Now;
        }
    }
    public class ApplicationDevicesModel : BaseModel<ApplicationDevices>
    {
        public ApplicationDevicesModel()
        {
            table = ApplicationDevices.TableName;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Insert(ApplicationDevices obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter("appid",obj.appid),
					new SqlParameter("deviceid",obj.deviceid),
					new SqlParameter("gcmid",obj.gcmid),
                    new SqlParameter("createdate",obj.createdate),
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Update(ApplicationDevices obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter("appid",obj.appid),
					new SqlParameter("deviceid",obj.deviceid),
					new SqlParameter("gcmid",obj.gcmid),

                }, new SqlParameter[]{
					new SqlParameter("id",obj.id),
															 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Delete(int id)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter("id",id),
															 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool DeleteMulti(string ListID)
        {
            try
            {
                if (ListID != string.Empty)
                {
                    foreach (string ID in ListID.Split(','))
                    {
                        Delete(int.Parse(ID));
                    }
                }
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<ApplicationDevices> GetByPage(int page, int pagesize, List<ApplicationDevices> source)
        {
            List<ApplicationDevices> list = new List<ApplicationDevices>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
        public List<ApplicationDevices> GetListBy(DataTable dt)
        {
            List<ApplicationDevices> list = new List<ApplicationDevices>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ApplicationDevices obj = new ApplicationDevices();
                    obj.id = (dr["id"] is DBNull) ? 0 : int.Parse(dr["id"].ToString());
                    obj.appid = (dr["appid"] is DBNull) ? string.Empty : dr["appid"].ToString();
                    obj.deviceid = (dr["deviceid"] is DBNull) ? string.Empty : dr["deviceid"].ToString();
                    obj.gcmid = (dr["gcmid"] is DBNull) ? string.Empty : dr["gcmid"].ToString();
                    obj.createdate = (dr["createdate"] is DBNull) ? DateTime.Now : DateTime.Parse(dr["createddate"].ToString());
                    list.Add(obj);
                }
            }
            return list;
        }
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public ApplicationDevices GetBy(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ApplicationDevices obj = new ApplicationDevices();
                    obj.id = (dr["id"] is DBNull) ? 0 : int.Parse(dr["id"].ToString());
                    obj.appid = (dr["appid"] is DBNull) ? string.Empty : dr["appid"].ToString();
                    obj.deviceid = (dr["deviceid"] is DBNull) ? string.Empty : dr["deviceid"].ToString();
                    obj.gcmid = (dr["gcmid"] is DBNull) ? string.Empty : dr["gcmid"].ToString();
                    obj.createdate = (dr["createdate"] is DBNull) ? DateTime.Now : DateTime.Parse(dr["createdate"].ToString());
                    return obj;
                }
            }
            return new ApplicationDevices();
        }
        public ApplicationDevices GetByID(int id)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter("id",id),
															
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public ApplicationDevices GetByAppidDeviceid(string appid, string deviceid)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter("appid",appid),
					new SqlParameter("deviceid",deviceid),									
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public int GetCountDevice(string appid, string fromtime, string totime)
        {
            DateTime fromdate = totime.ToDateTime();
            DateTime todate = (totime + " 23:59:59").ToDateTime();

            string query = "select * from " + table + " where appid=@appid and createdate>=@fromdate and createdate<=@todate";
            int count = GetCountBy(query, new SqlParameter[] {
                new SqlParameter("appid",appid),
                new SqlParameter("fromdate",fromdate),
                new SqlParameter("todate",todate)
            });
            return count;
        }
    }
}
